package com.bawei.module_moto_circle.ui.post

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import androidx.paging.filter
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.module_moto_circle.model.PostInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.shareIn
import javax.inject.Inject

/**
 *作者：lxy
 *功能:
 *时间：2025/6/1
 */
@HiltViewModel
class PostListViewModel @Inject constructor(private val mRepository: PostListRepository) :
    BaseViewModel() {

    private val mPostState_ = MutableStateFlow<RequestEvent>(Loading)
    val mPostState = mPostState_.asStateFlow()

    var mPostListData = listOf<PostInfo>()

    private val requestLikeEvent_ = MutableSharedFlow<Int>() //下标
    val requestLikeEvent = requestLikeEvent_.asSharedFlow()

    private val _removePostItemFlow = MutableStateFlow(mutableListOf<PostInfo>())
    private val removePostItemFlow: Flow<MutableList<PostInfo>> get() = _removePostItemFlow

    val mCheckIfHasFocus by lazy {
        MutableLiveData<Boolean>()
    }

    //分页参数
    private var isFocus: Int? = null
    private var keyword: String? = null
    private var memberId: String? = null
    private var sortType: String? = null
    private var topicId: String? = null

    private var pageNum = 1
    private var pageSize = 10

    //设置帖子分页参数
    fun setPageParam(
        sortType: String?,
        keyword: String?,
        memberId: String?,
        ifFocus: Int?,
        topicId: String?
    ) {

        this.sortType = sortType
        this.keyword = keyword
        this.memberId = memberId
        this.isFocus = ifFocus
        this.topicId = topicId
    }



    fun getPostInfo() {
        execute(
            action = mRepository.getPostInfo(
                pageNum,
                pageSize,
                isFocus,
                keyword,
                memberId,
                sortType,
                topicId
            ),
            onStart = {
                mPostState_.value = Loading
            },
            onFinish = {
                if (it.data.records.isNullOrEmpty()) {
                    mPostState_.value = Empty
                } else {
                    mPostListData = it.data.records
                    mPostState_.value = Success
                }
            },
            onError = {
                mPostState_.value = Failed(it)
            }
        )
    }

    val postPageEvent2
        get() =
        mRepository.queryPostPage(
            isFocus,
            keyword,
            memberId,
            sortType,
            topicId
        )
            .cachedIn(viewModelScope)
            .combine(removePostItemFlow) { data, removed ->
                data.filter {
                    !removed.any { remove -> remove.posts_id == it.posts_id }
                }
            }
            .shareIn(viewModelScope, SharingStarted.Lazily)


    fun checkIfHasFocus() {
        execute(
            action = mRepository.checkIfHasFocus(),
            onError = {
                mCheckIfHasFocus.value = false
            },
            onFinish = {
                mCheckIfHasFocus.value = it.data
            }
        )
    }

    /**
     * 点赞和取消
     */
    fun giveLike(postsId: String, position: Int) {
        execute(
            action = mRepository.giveLike(postsId),
            onFinish = {
                if (it.data) {
                    requestLikeEvent_.emit(position)
                } else {
                    requestLikeEvent_.emit(-1)
                }
            },
            onError = {
                requestLikeEvent_.emit(-1)
            }
        )
    }

    fun followUser() {

    }

    fun cancelFollow() {

    }
}